const api = require('../../../utils/api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isApply:"",
    jobdetails: {},
    clickcnt: "展开",
    height: "",
    companyName: "",
    workType: "",
    icondic: "unfold",
    showbtn: false,
    opentype: "",
    showCalendar: "",
    dialogShow: false,
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    startDay: "",
    endDay: "",
    benifit: "",
    resumepart: "",
    isAuthorize: false,
    partTimeId: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    that.setData({
      partTimeId: options.partTimeId
    })
    getApp().globalData.get("/api/part_time/detail/" + options.partTimeId, {
      openid: wx.getStorageSync("openid").length > 0 ? wx.getStorageSync("openid"):""
    }).then(res => {
      if (res.code != 200){
        wx.showToast({
          title: res.message,
          icon: "none"
        })
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/recruit/personal/personal',
          })
        },2200)
      }else {
        that.setData({
          companyName: res.data.companyName,
          workType: res.data.workType,
          jobdetails: res.data.partTime,
        })
        if (res.data.isApply != undefined){
          that.setData({
            isApply: res.data.isApply
          })
        }
        if (res.data.partTime.benifit != "") {
          that.setData({
            benifit: res.data.partTime.benifit.split(","),
          })
        }
        if (res.data.partTime.workDay != "") {
          that.setData({
            startDay: res.data.partTime.workDay.split("至")[0],
            endDay: res.data.partTime.workDay.split("至")[1],
          })
        }
      }
    })
    let token = wx.getStorageSync("token");
    if (token) {
      // 获取个人简历的信息
      getApp().globalData.get("/api/resume", {}).then(res => {
        let progess = 0;
        if (res.data.resume.tel != "") {
          progess = progess + 20;
        }
        if (res.data.educationals.length != 0) {
          progess = progess + 20;
        }
        if (res.data.workExperiences.length != 0) {
          progess = progess + 20;
        }
        if (res.data.certificates.length != 0) {
          progess = progess + 20;
        }
        if (res.data.resume.comment != "") {
          progess = progess + 20;
        }
        that.setData({
          resumepart: progess,
          isAuthorize: true
        })
      })
    } else {
      that.setData({
        opentype: "getUserInfo"
      })
    }
  },
  onShow: function() {

  },
  onReady: function() {
    const that = this;
    const query = wx.createSelectorQuery();
    query.select('#job_des').boundingClientRect(function(res) {
      if (parseInt(res.height) > 260) {
        that.setData({
          height: "504rpx",
          icondic: "unfold",
          showbtn: true
        })
      }
    }).exec();
  },
  tapDialogButton(e) {
    console.log("开始跳转到发送简历页面。")
    this.setData({
      dialogShow: false,
      showOneButtonDialog: false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    const that = this;
    return {
      title: that.data.jobList.title,
      path: '/pages/details/details?id=123'
    }
  },
  tomap: function(e) {
    const that = this;
    wx.openLocation({
      latitude: that.data.jobdetails.latitude * 1,
      longitude: that.data.jobdetails.longitude * 1,
      scale: 20,
    })
  },
  switchCnt: function() {
    const that = this;
    if (that.data.height != "auto") {
      that.setData({
        height: "auto",
        clickcnt: "收起",
        icondic: "fold"
      })
    } else {
      that.setData({
        height: "504rpx",
        clickcnt: "展开",
        icondic: "unfold"
      })
    }
  },
  getUserInfo: function (res) {
    const that = this;
    getApp().globalData.get("/api/wx/info", {
      sessionKey: wx.getStorageSync("sessionKey"),
      signature: res.detail.signature,
      rawData: res.detail.rawData,
      encryptedData: res.detail.encryptedData,
      iv: res.detail.iv
    }).then(res => {
      console.log(res)
      wx.setStorageSync('token', res.data.token);
      // 获取个人简历的信息
      getApp().globalData.get("/api/resume", {}).then(res => {
        let progess = 0;
        if (res.data.resume.tel != "") {
          progess = progess + 20;
        }
        if (res.data.educationals.length != 0) {
          progess = progess + 20;
        }
        if (res.data.workExperiences.length != 0) {
          progess = progess + 20;
        }
        if (res.data.certificates.length != 0) {
          progess = progess + 20;
        }
        if (res.data.resume.comment != "") {
          progess = progess + 20;
        }
        that.setData({
          resumepart: progess,
          isAuthorize: true
        })
      })
    })
  },
  showCalendar: function() {
    const that = this;
    if (that.data.jobdetails.tel == "") {
      wx.showToast({
        title: "请完善您的手机号",
        icon: "none"
      })
    } else if (that.data.resumepart < 20) {
      wx.showToast({
        title: "请完善您个人简历",
        icon: "none"
      })
    } else {
      getApp().globalData.post("/api/user/apply",{
        partTimeId: that.data.partTimeId
      }).then(res=>{
        if (res.code == 200){
          wx.showToast({
            title: "报名成功,请耐心等待商家审核",
            icon: "none"
          })
          that.setData({
            isApply: true
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: "none"
          })
        }
      })
    }
  }
})